#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：BroadviewCoss
@File    ：redis_task.py
@IDE     ：PyCharm
@Author  ：kdly
@Date    ：2025/4/28 上午12:14
'''
import time

from django_redis import get_redis_connection
import json
import logging

logger = logging.getLogger(__name__)


def publish_task_operation(action, task_id):
    """
    发布任务操作到Redis频道
    :param action: 操作类型 enable/disable
    :param task_id: 任务ID
    """
    try:
        conn = get_redis_connection("default")
        message = json.dumps({
            "action": action,
            "task_id": task_id,
            "timestamp": time.time()
        })
        conn.publish('task_operations', message)
        logger.info(f"已发布任务操作: {action} {task_id}")
    except Exception as e:
        logger.error(f"发布任务操作失败: {str(e)}")
        raise


def get_task_listener():
    """获取已配置的Redis监听连接"""
    return get_redis_connection("default")
